

const { mysql } = require('../qcloud')


/**
 * 修改用户的glod
 */
async function post(ctx, next) {
  /**
   * 解析微信发送过来的请求体
   * 可查看微信文档：https://mp.weixin.qq.com/debug/wxadoc/dev/api/custommsg/receive.html#接收消息和事件
   */
  const body = ctx.request.body
  const openId = body.open_id
  const preGold = body.gold
  const res = await mysql('record').select('*').where('open_id', openId).limit(1)
  ctx.state.data = res
  if (res && res.length === 1) {
    const resRecord = res[0]
    const currentGold = resRecord.gold + preGold > 0 ? resRecord.gold + preGold : 0
    const resUpdate = await mysql('record').update('gold', currentGold).where('open_id', openId)
    ctx.state.data = resUpdate
  }
}

module.exports = {
  post
}
